//
//  VVYChangeCoordinate.c
//  viviyoo
//
//  Created by viviyoo on 15/12/9.
//  Copyright © 2015年 viviyoo. All rights reserved.
//

#include "VVYChangeCoordinate.h"
#include <math.h>

void bd_encrypt(double gg_lat, double gg_lon, double *bd_lat, double *bd_lon)
{
    double x = gg_lon, y = gg_lat;
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI);
    double theta = atan2(y, x) + 0.000003 * cos(x * M_PI);
    *bd_lon = z * cos(theta) + 0.0065;
    *bd_lat = z * sin(theta) + 0.006;
}

void bd_decrypt(double bd_lat, double bd_lon, double *gg_lat, double *gg_lon)
{
    
    double x = bd_lon - 0.0065, y = bd_lat - 0.006;
    
    double z = sqrt(x * x + y * y) - 0.00002 * sin(y * M_PI);
    
    double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);
    
    *gg_lon = z * cos(theta);
    
    *gg_lat = z * sin(theta);
    
}
